package com.ruoyi.message.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.message.domain.Coach;
import com.ruoyi.message.domain.Course;
import com.ruoyi.message.domain.OrderInfo;
import com.ruoyi.message.service.ICoachService;
import com.ruoyi.message.service.ICourseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.message.service.IOrderInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约Controller
 * 
 * @author zsc
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/message/order")
public class OrderInfoController extends BaseController {
    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private ICoachService coachService;

    @Autowired
    private ICourseService courseService;
    /**
     * 查询预约列表
     */
    @PreAuthorize("@ss.hasPermi('message:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo) {
        startPage();
        if (orderInfo.getCoachName()!=null) {
            Coach coach = coachService.selectCoachByName(orderInfo.getCoachName());
            List<OrderInfo> list = orderInfoService.selectOrderByCoach(coach.getId());
            for (int i=0;i<list.size();i++){
                OrderInfo order = list.get(i);
                Course course = courseService.selectCourseById(order.getProgramId());
                order.setCoachName(coach.getName());
                order.setProgramName(course.getName());
                list.set(i, order);
            }
            return getDataTable(list);
        }

        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        for (int i=0;i<list.size();i++){
            OrderInfo order = list.get(i);
            Coach coach = coachService.selectCoachById(order.getCoachId());
            Course course = courseService.selectCourseById(order.getProgramId());
            order.setCoachName(coach.getName());
            order.setProgramName(course.getName());
            list.set(i, order);
        }
        return getDataTable(list);
    }

    /**
     * 导出预约列表
     */
    @PreAuthorize("@ss.hasPermi('message:order:export')")
    @Log(title = "预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo) {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "预约数据");
    }

    /**
     * 获取预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(orderInfoService.selectOrderInfoById(id));
    }

    /**
     * 新增预约
     */
    @PreAuthorize("@ss.hasPermi('message:order:add')")
    @Log(title = "预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo) {
        return toAjax(orderInfoService.insertOrderInfo(orderInfo));
    }

    /**
     * 修改预约
     */
    @PreAuthorize("@ss.hasPermi('message:order:edit')")
    @Log(title = "预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo) {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 删除预约
     */
    @PreAuthorize("@ss.hasPermi('message:order:remove')")
    @Log(title = "预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(orderInfoService.deleteOrderInfoByIds(ids));
    }
}
